package com.fanli.scheduler.controller;

import com.fanli.scheduler.utils.CommonMessage;
import com.fanli.scheduler.utils.CommonUtils;
import com.fanli.scheduler.utils.Constant;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by wei.shen on 2015/9/1.
 */
@SuppressWarnings("unused")
@Controller
public class LoginController {
    private static Logger logger = Logger.getLogger(LoginController.class);

    @RequestMapping(value = "/login")
    @ResponseBody
    public CommonMessage login(@RequestParam("username") String username,@RequestParam("password") String password,HttpServletRequest request) {
    	//暂时写死
    	String user="bigdata";
    	String pass="51fanli";
//    	System.err.println(username+" "+password);
    	CommonMessage msg=new CommonMessage();
    	if(CommonUtils.isNotEmpty(username)&&CommonUtils.isNotEmpty(password)){
    		if(user.equals(username)&&pass.equals(password)){
    			msg.setSuccess(true);
    			msg.setMsg("/fanli/index");
    			HttpSession session = request.getSession();
    			session.setAttribute(Constant.AUTH_USER_KEY, user);
    		}else{
    			msg.setSuccess(false);
    			msg.setMsg("账号信息错误 ，请确认后登录！！");
    		}
    	}else{
    		msg.setSuccess(false);
    		msg.setMsg("输入的账号信息存在空值");
    	}
        return msg;
    }
    
    @RequestMapping(value = "/isLogin")
    @ResponseBody
    public CommonMessage isLogin(HttpServletRequest request) {
    	//暂时写死
    	
    	HttpSession session = request.getSession();
		String user=(String) session.getAttribute(Constant.AUTH_USER_KEY);
    	CommonMessage msg=new CommonMessage();
    	if(CommonUtils.isNotEmpty(user)){
    		msg.setSuccess(true);
    	}else{
    		msg.setSuccess(false);
    		msg.setMsg("输入的账号信息存在空值");
    	}
        return msg;
    }
}
